//登录协议管理
class LoginPrase{
    private static _instance:LoginPrase = null;
    private _isAccredit:boolean = false;
	/** 
	 * 是否授权
	 */
	public get isAccredit(){
		return this._isAccredit;
	}


    constructor()
    {

    }

    public static instance()
    {
        if(!this._instance)
        {
            this._instance = new LoginPrase();
        }
        return this._instance;
    }

    //登录
    public Login()
    {
        // WeChatgGameManager.instance().getUserInfo((info)=>{
        //     this._isAccredit = true;
            // WeChatgGameManager.instance().hideUserInfoButton();
            this.postRoleInfo();
        // }, this); 
    }

    //请求玩家数据
    public postRoleInfo()
    {
        // let userInfo = WeChatgGameManager.instance().userInfo;
        // let nick_name = "我是丁进";
        // let avatar_url = "https://wx.qlogo.cn/mmopen/vi_32/XkkpCONgGIC2oA3cVRYkF0OyHonYs0M4DySGCtUCh8G5d3n5mMnl5PIB0hZF6pvU0bMvlgZpNCGw8LYDhicgdrQ/132";
        // if(userInfo)
        // {   
        //     nick_name = userInfo['nickName'] != undefined ? userInfo['nickName']: nick_name;
        //     avatar_url = userInfo['avatarUrl'] != undefined ? userInfo['avatarUrl']: avatar_url;
        // }
        
        let user = {
            // nick_name:nick_name,
            // avatar:avatar_url,
            version:GameConst.version
        }
        
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warrioruser/get_user_info', StringUtil.jsonToHttpPostString(user), (e)=>{
            let data = JSON.parse(e);
            let showData =   WeChatgGameManager.instance().getOnShowData();
            if(showData)    
            {
                showData.then((e)=>{
                    if(e && e['query'] && e['query']['from_openid'])
                    {
                        let queryData = {
                            from_openid : e['query']['from_openid']
                        }
                        HttpManager.instance().sendPost(GameConst.httpUrl + 'warrioruser/handle_invite' , StringUtil.jsonToHttpPostString(queryData), (e)=>{
                            
                        })
                    } 
                })
            }
            
            WeChatgGameManager.instance().sendALDEvent('versionFrom', 'version', GameConst.version);
            RoleCache.instance().setRoleData(data.data);
            UIManager.instance().openUILogic(MainUI);	
            SoundManager.instance().init();
        }, this);
    }

    //登出
    public LogOut()
    {
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warrioruser/update_logout_time', null, (e)=>{
            
        }, this);
    }
}